class Solution:
    def minPathSum(self , matrix ):
        for i in range(len(matrix)):
            for j in range(len(matrix[0])):
                if i == j == 0: continue
                elif i == 0:  matrix[i][j] = matrix[i][j - 1] + matrix[i][j]
                elif j == 0:  matrix[i][j] = matrix[i - 1][j] + matrix[i][j]
                else: matrix[i][j] = min(matrix[i - 1][j], matrix[i][j - 1]) + matrix[i][j]
        return matrix[-1][-1]